package com.jxs.android.weixi;

import java.util.List;
import java.util.TreeMap;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.jxs.android.util.HandlerHolder;
import com.jxs.android.util.StartHelper;
import com.jxs.android.weiget.BuOweDetailView;
import com.jxs.android.weiget.BuOweDetailView.LoadingOweDataListener;
import com.jxs.android.weiget.PopMsg;
import com.jxs.android.weiget.TopBar;
import com.jxs.android.weixi.data.CustBaseInfoNew;
import com.jxs.android.weixi.data.OweFeeDetailInfo;
import com.jxs.android.weixi.data.OweFeeResultInfo;
import com.telecom.weixi.R;
import com.telecom.weixi.WeixiApplication;
import com.telecom.weixi.view.BCustomerView;

public class Search_Of_OweFee_Detail extends AbstractActivity implements LoadingOweDataListener {

	private int PAGE_SIZE = 10;
	private int currentPage = 1;

	private ProgressDialog progressDialog;
	private BuOweDetailView bu_owe_detail_view;
	private OweFeeResultInfo oweFeeResultInfo;
	private LinearLayout bu_customer_view;

	private Search_GetCustBaseInfoNewHolder getCustBaseInfoNewHolder;
	private Search_GetOweDetailInfoHolder getOweDetailInfoHolder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.bu_search_owe_fee_detail_view);
		progressDialog = new ProgressDialog(this);
		initLayouts();
		if (null != getIntent() && null != getIntent().getExtras()) {
			oweFeeResultInfo = (OweFeeResultInfo) getIntent().getExtras().getSerializable("oweFeeResultInfo");
			query();
		}
	}

	private void initLayouts() {
		bu_customer_view = (LinearLayout) findViewById(R.id.bu_customer_view);
		bu_owe_detail_view = (BuOweDetailView) findViewById(R.id.bu_owe_detail_view);
		bu_owe_detail_view.setDataListener(this);
		getCustBaseInfoNewHolder = new Search_GetCustBaseInfoNewHolder(handler, 0);
		getOweDetailInfoHolder = new Search_GetOweDetailInfoHolder(handler, 1);
	}

	@Override
	public void loadingData(int currentPage) {
		loadingOweFeeDetailInfo(currentPage);
	}

	private void loadingOweFeeDetailInfo(final int currentPage) {
		progressDialog = ProgressDialog.show(this, "", getString(R.string.prompt_loading), true);
		getOweDetailInfoHolder.getOweInfoDetail(WeixiApplication.getUserInfo().getUserId(), oweFeeResultInfo.serv_id, PAGE_SIZE, currentPage);
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		initTopBarReturn(topBar);
		topBar.hindBtn();
		topBar.setTitle(R.string.search_owe);
	}

	private void query() {
		progressDialog = ProgressDialog.show(this, "", getString(R.string.prompt_loading), true);
		getCustBaseInfoNewHolder.getCustBaseInfoNew(WeixiApplication.getUserInfo().getTicket(), oweFeeResultInfo.cust_id, oweFeeResultInfo.serv_id);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			if (msg.what == HandlerHolder.What.ERROR_MSG) {
				if (null != progressDialog && progressDialog.isShowing()) {
					progressDialog.dismiss();
				}
				PopMsg.show(msg.obj.toString());
			} else if (msg.what == HandlerHolder.What.TO_LOGIN) {
				PopMsg.show(msg.obj.toString());
				StartHelper.startActivity(Login.class);
				finish();
			} else if (msg.what == HandlerHolder.What.UPDATE_LIST_UI) {
				if (msg.arg1 == 0) {
					updateCustBaseInfoNewList(msg);
					currentPage = 1;
					loadingOweFeeDetailInfo(currentPage);
				} else if (msg.arg1 == 1) {
					updateOweFeeDetailInfoList(msg);
				}
			}
		}
	};

	private void updateOweFeeDetailInfoList(Message msg) {
		@SuppressWarnings("unchecked")
		List<OweFeeDetailInfo> tems = (List<OweFeeDetailInfo>) msg.obj;
		boolean flag = false;
		bu_owe_detail_view.getLoadMoreButton().setEnabled(true);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				bu_owe_detail_view.setEmptyVisibility(View.VISIBLE);
				bu_owe_detail_view.getLoadMoreButton().setEnabled(false);
			}
		}
		bu_owe_detail_view.setOweFeeDetailInfos(tems);
		if ((tems.size() < PAGE_SIZE) || flag) {
			bu_owe_detail_view.getLoadMoreButton().setText("数据已全部获取完毕");
			bu_owe_detail_view.getLoadMoreButton().setEnabled(false);
		} else {
			bu_owe_detail_view.getLoadMoreButton().setText("查看更多...");
			currentPage++;
			bu_owe_detail_view.setCurrentPage(currentPage);
		}
	}

	private void updateCustBaseInfoNewList(Message msg) {
		bu_customer_view.removeAllViews();
		BCustomerView customerView = new BCustomerView(this);
		customerView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		@SuppressWarnings("unchecked")
		TreeMap<String, CustBaseInfoNew> mCustInfos = (TreeMap<String, CustBaseInfoNew>) msg.obj;
		Log.e("ATG", mCustInfos.toString());
		String name = "", type = "", manager = "";
		StringBuffer kehuNumBuffer = new StringBuffer("");
		StringBuffer zhanghuNumBuffer = new StringBuffer("");
		String zhanghuList = "";
		String kehulist = "";
		for (String key : mCustInfos.keySet()) {
			if ("2".equals(key)) {// @2 = "";// '客户名称'
				name = mCustInfos.get(key).content;
			} else if ("3".equals(key)) {// @3 = "";// '客户层客户类型'
				type = mCustInfos.get(key).content;
			} else if ("4".equals(key)) {// @4 = "";// '客户层客户经理'
				manager = mCustInfos.get(key).content;
			} else if ("5".equals(key)) {
				/**
				 * @5 = "";// '客户产品数'
				 * @6 = "";// '移动'
				 * @7 = "";// '固话'
				 * @8 = "";// '宽带'
				 * @9 = "";// '其它'
				 */
				kehuNumBuffer.append(mCustInfos.get(key).content + " 【");
			} else if ("6".equals(key)) {
				kehuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("7".equals(key)) {
				kehuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("8".equals(key)) {
				kehuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("9".equals(key)) {
				kehuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content + "】");
			} else if ("16".equals(key)) {
				/**
				 * @16 = "";// '账户产品数'
				 * @17 = "";// '移动'
				 * @18 = "";// '固话'
				 * @19 = "";// '宽带'
				 * @20 = "";// '其它'
				 */
				zhanghuNumBuffer.append(mCustInfos.get(key).content + " 【");
			} else if ("17".equals(key)) {
				zhanghuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("18".equals(key)) {
				zhanghuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("19".equals(key)) {
				zhanghuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("20".equals(key)) {
				zhanghuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content + "】");
			} else if ("21".equals(key)) {// @21 = "";// '账户产品清单'
				zhanghuList = mCustInfos.get(key).content;
			} else if ("22".equals(key)) {// @22 = "";// '客户产品清单'
				kehulist = mCustInfos.get(key).content;
			} else {
				if (!"1".equals(key)) {
					//此处节省空间
					if("10".equals(key)){
						customerView.addRow(mCustInfos.get(key).title, mCustInfos.get(key).content);
					}
				}
			}

		}
		customerView.fixContent(name, type, manager);
	/*	if (mCustInfos.containsKey("5")) {
			customerView.addRow(mCustInfos.get("5").title, kehuNumBuffer.toString());
			customerView.addRow(mCustInfos.get("5").title, kehulist, true);
		}
		if (mCustInfos.containsKey("16")) {
			customerView.addRow(mCustInfos.get("16").title, zhanghuNumBuffer.toString());
			customerView.addRow(mCustInfos.get("16").title, zhanghuList, true);
		}*/
		customerView.addRow(oweFeeResultInfo.total_owe_charge, oweFeeResultInfo.total_owe_month);
		bu_customer_view.addView(customerView);

	}

}
